package controllers;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;

import supers.GUIController;
import utils.Updater;
import views.SisUpdGUI;

public class SisUpdController extends GUIController{
	
	public SisUpdGUI GUI;
	private Updater updater;

	public SisUpdController(SisDskController sisDskController, Updater updater) {
		super(sisDskController, SisUpdController.class.getName());
		this.updater = updater;
		GUI = (SisUpdGUI) super.configGUI(new SisUpdGUI(), new GUIListener(), new GUIListener(), new GUIListener());
		EventQueue.invokeLater(new Runnable() {
		      public void run() {
				GUI.setVisible(true);
			}
		});
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub		
	}

	@Override
	public boolean validado() {
		// TODO Auto-generated method stub
		return false;
	}

	public String getStatus() {
		return GUI.lbStatus.getText();
	}
	public void setStatus(String status){
		GUI.lbStatus.setText(status);
	}

	public void setProgresso(int porc){
		GUI.pbDownload.setValue(porc);
	}
	
    class GUIListener implements ActionListener, FocusListener, MouseListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			Object src = arg0.getSource();
			if(src == GUI.btCancelar){
				if(GUI.btCancelar.getText().equals("Cancelar")){
					updater.stopUpdate();
					GUI.dispose();
					try {
						finalize();
					} catch (Throwable e) {
						JOptionPane.showMessageDialog(null, e.getMessage());
					}
				}
				if(GUI.btCancelar.getText().equals("Atualizar")){
					try {
						Runtime.getRuntime().exec("cmd /c start update.bat");
						GUI.dispose();
						try {
							finalize();
						} catch (Throwable e) {
							e.printStackTrace();
						}
						sisDskController.fechar();
					} catch (Exception e) {
						JOptionPane.showMessageDialog(null, e.getMessage());
					}
				}
			}
		}
    	
		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void focusGained(FocusEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void focusLost(FocusEvent arg0) {
			// TODO Auto-generated method stub
			
		}

    }

}
